<?php
$del = isset($_GET['del']) ? $_GET['del'] : "";
if (!empty($del)) {
//    $delete = mysql_query("DELETE porderi FROM tbl_order_item porderi, tblproduct_in_pharmacy pip WHERE porderi.pd_id = pip.pid AND porderi.od_id= '" . $del . "' AND pip.pharmacy_id = '" . $_SESSION['acc_id'] . "' ");
      $delete = mysql_query("UPDATE tbl_order_item porderi, tblproduct_in_pharmacy pip
                            SET porderi.delete_orderRow_status = 0
                            WHERE pip.pid=porderi.pd_id
                            AND porderi.od_id = '" . $del . "'
                            AND pip.pharmacy_id = '" . $_SESSION['acc_id'] . "' ");

    header('Location: order/index.php');
}

if (!isset($sp))
    $sp = explode("/", $_SERVER['PHP_SELF']);

if ($_SERVER['HTTP_HOST'] == "localhost" || $_SERVER['HTTP_HOST'] == "192.168.1.34") {
    $sp[1] = '/' . $sp[1];
}
else
    $sp[1] = '';

$w_root = $_SERVER['SERVER_NAME'] . $sp[1];
$w_root = str_replace('//', '/', $w_root);
$w_root = str_replace('\\\\', '\\', $w_root);

$s_root = $_SERVER['DOCUMENT_ROOT'] . $sp[1];
$s_root = str_replace('//', '/', $s_root);
$s_root = str_replace('\\\\', '\\', $s_root);
/* ========End Setting up web root and server root ============= */
require_once $s_root . '/libraryfiles/config.php';



$include = '
<script type="text/javascript" src="' . W_ROOT . '/js/common.js"></script>
<script type="text/javascript" src="' . W_ROOT . '/js/jquery.js"></script>
';
?>
<?php include(S_ROOT . '/company/header_top.php'); ?>
<body>
    <?php
    include(S_ROOT . '/includefiles/navmenu.php');
    ?>
    <!-- End Header and Nav -->
    <div class="clear"></div>
    <?php include(S_ROOT . '/company/header_admin.php'); ?>
    <!------------the end header from include----------------->
    <div class="row"> 
        <div class="nine columns">
            <?php
            if (isset($_SESSION['message'])) {
                ?>
                <div class="alert alert-success fade in">
                    <button data-dismiss="alert" class="close" type="button">x</button>
                    <h3 class="alert-heading"><!--Well done! You successfully.--><?php echo $lang['SUCCESSSMS']; ?></h3>
                    <p style="font-size:16px;"><blink><?php echo $_SESSION['message']; ?></blink></p>
                </div>
                <?php
                unset($_SESSION['message']);
            }
            ?>

            <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
            <style>
                .toggler
                {
                    margin: 10px 0 0 0;
                }
                .button {		
                    text-decoration: none;        
                }
                .point
                {
                    cursor: pointer;
                }
                #effect {
                    position: relative;
                    padding: 0.4em;
                }
                h3.statistic {
                    margin: 0;
                    padding: 0.4em;
                    text-align: left;
                    font-size: 14px;
                    display:block;
                }
                .t_none
                {
                    border:none;
                }
                .notread
                {
                    color:#000;
                    font-weight: bold;
                }
                .read
                {
                    color:#999;
                    font-weight:normal;
                    background:#eee!important;
                }
                a {color:#000;}
                tr:hover
                {
                    background: #ddd!important;
                }
                .pading_status:hover{
                    color: blue!important;
                }
            </style>
            <div style="margin: 10px 15px 0 0;">
                <h3 class="statistic ui-widget-header ui-corner-all"><!--Order--><?php echo $lang['SELL_ORDER']; ?></h3>
                <form name="frmlist">
                    <table cellpadding="0" cellspacing="0" border="0" width="100%" class="table">
                        <tr>
                            <th width="5%" align="center">No</th>
                            <th width="14%" align="center">Buyer</th>
                            <th width="18%" align="center">Address</th>
                            <th width="17%" align="center">Phone</th> 
                            <th width="35%" align="center">Email</th>
                            <th width="17%" align="center">Date</th>
                            <th width="17%" align="center">Status</th>
                            <th width="17%" align="center">Action</th>
                        </tr>
                    </table>

                    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

                        <?php
                        $qstring = '';
                        $rowsPerPage = isset($_GET['num']) ? $_GET['num'] : '10';
                        $sql = "SELECT * FROM tbl_order_item AS porderi
                                INNER JOIN tbl_order AS porder ON porderi.od_id=porder.od_id
                                INNER JOIN tblproduct_in_pharmacy AS pip ON pip.pid=porderi.pd_id
                                INNER JOIN tblproduct AS pro ON pro.id=pip.product_id
                                WHERE pip.pharmacy_id = '" . $_SESSION['acc_id'] . "' AND porderi.delete_orderRow_status = 1 GROUP BY porder.od_id ORDER BY porder.od_id ASC";
                        $ch_ord = mysql_query(getPagingQuery($sql, $rowsPerPage));
                        $pagingLink = getPagingLink($sql, $rowsPerPage, $qstring);
                        $i = 1;
                        $n = 0;

                        $page = (!empty($_GET['page'])) ? $_GET['page'] : '0';
                        $nomlist = (!empty($page)) ? $rowsPerPage * $page - 10 : '0';
                        while ($rows = mysql_fetch_assoc($ch_ord)) {
                            $n++;
                            extract($rows);
                            if ($i % 2) {
                                $class = 'row1';
                            } else {
                                $class = 'row2';
                            }
                            ?>           
                            <tr class="<?php echo $class; ?>">
                                <td width="5%" align="center"><a href="<?php echo W_ROOT; ?>/company/action/?p=order/order&id=<?php echo $rows['od_id']; ?>"><?php echo $n + $nomlist; ?></a></td>
                                <td width="15%" align="center"><a href="<?php echo W_ROOT; ?>/company/action/?p=order/order&id=<?php echo $rows['od_id']; ?>"><?php echo $rows['od_shipping_first_name'] . " " . $rows['od_shipping_last_name']; ?></a></td>
                                <td width="20%" align="center"><a href="<?php echo W_ROOT; ?>/company/action/?p=order/order&id=<?php echo $rows['od_id']; ?>"><?php echo $rows['od_shipping_address']; ?></a></td>
                                <td width="15%" align="center"><a href="<?php echo W_ROOT; ?>/company/action/?p=order/order&id=<?php echo $rows['od_id']; ?>"><?php echo $rows['od_shipping_telephone']; ?></a></td>               
                                <td width="15%" align="center"><a href="<?php echo W_ROOT; ?>/company/action/?p=order/order&id=<?php echo $rows['od_id']; ?>"><?php echo $rows['od_shipping_email']; ?></a></td>                               
                                <td width="17%" align="center"><a href="<?php echo W_ROOT; ?>/company/action/?p=order/order&id=<?php echo $rows['od_id']; ?>"><?php echo $rows['od_date']; ?></a></td>
                                <td width="10%" align="center">
                                    <?php
                                    if ($rows['oi_status'] == 0) {
                                        echo "<a href='" . W_ROOT . "/company/action/?p=order/index&status=" . $rows['od_id'] . "' style='color: red' class='pading_status'>";
                                        echo 'Pending';
                                    } else if ($rows['oi_status'] == 1) {
                                        echo '<a style="color: blue; font-weight:bold">';
                                        echo 'Sold';
                                    }
                                    ?>
                                    </a></td>
                                <td style="width: 10px;padding:10px"><a title="delete" href="<?php echo W_ROOT; ?>/company/action/?p=order/index&del=<?php echo $rows['od_id']; ?>"><i class="icon-remove"></i></a></td>
                            </tr>
                            <?php $i++;
                        }
                        ?>

                    </table>
                    <div class="row-fluid">
                        <div class="span7">
                            <div class="wp-pagenavi">
<?php echo $pagingLink; ?>
                                <div class="clear"></div>
                            </div>
                        </div>
                        <div class="span3 offset2">
                            view per page
                            <select class="right span4" name="numpage" onchange="javascript:viewByNumPage();">
                                <option value="10" <?php echo($rowsPerPage == 10) ? ' selected="selected"' : ''; ?>>10</option>
                                <option value="20" <?php echo($rowsPerPage == 20) ? ' selected="selected"' : ''; ?>>20</option>
                                <option value="30" <?php echo($rowsPerPage == 30) ? ' selected="selected"' : ''; ?>>30</option>
                                <option value="40" <?php echo($rowsPerPage == 40) ? ' selected="selected"' : ''; ?>>40</option>
                                <option value="50" <?php echo($rowsPerPage == 50) ? ' selected="selected"' : ''; ?>>50</option>        
                            </select>
                        </div>            
                    </div> 
                </form>
            </div>

            <?php
//start update status from padding to sold
            $ch_status = isset($_GET['status']) ? $_GET['status'] : "";
//echo $ch_status;die();
            if (!empty($ch_status)) {
                $update = mysql_query("UPDATE tbl_order_item porderi, tbl_order porder, tblproduct_in_pharmacy pip
                            SET porderi.oi_status = 1
                            WHERE porderi.od_id= porder.od_id
                            AND pip.pid=porderi.pd_id
                            AND porderi.od_id = '" . $ch_status . "'
                            AND pip.pharmacy_id = '" . $_SESSION['acc_id'] . "'");

                header('Location: order/index.php');


                $select_ord = mysql_query("SELECT * FROM tbl_order_item AS porderi
                                INNER JOIN tbl_order AS porder ON porderi.od_id=porder.od_id
                                INNER JOIN tblproduct_in_pharmacy AS pip ON pip.pid=porderi.pd_id
                                INNER JOIN tblproduct AS pro ON pro.id=pip.product_id                                
                                WHERE porder.od_id = '" . $ch_status . "'");
                while ($row = mysql_fetch_assoc($select_ord)) {                   

                    // Fidelity card function
                    $count_fidel = mysql_query("SELECT * FROM fidelity_card WHERE user_id = '" . $row['user_id'] . "' AND owner = '" . $_SESSION['acc_id'] . "'");
                    $count_fidelity = mysql_num_rows($count_fidel);

                    if ($count_fidelity == 0) {
                        $count_ins = "INSERT INTO fidelity_card(user_id, owner, session_id, count_number) VALUES('" . $row['user_id'] . "', '" . $_SESSION['acc_id'] . "' , '" . $row['user_session_id'] . "', 1)";
                        $count_in = mysql_query($count_ins);
                    } else {
                        while ($rows = mysql_fetch_assoc($count_fidel)) {
                            if ($rows['session_id'] != $row['user_session_id']) {

                                $update_fidel = mysql_query("UPDATE fidelity_card SET count_number = count_number+1 , session_id = '" . $row['user_session_id'] . "' WHERE user_id = '" . $row['user_id'] . "' AND owner = '" . $_SESSION['acc_id'] . "'");
                            } else if ($rows['session_id'] == $row['user_session_id']) {
                                
                            }
                        } // End while
                    }
                } // End while
            }

//$content = 'order/index.php';
            ?>
        </div>
        <div class="three columns">
            <?php
            //include(S_ROOT.'/company/dashboard/statistic/showclick.php');
            include(S_ROOT . '/company/left.php');
            ?>
        </div>   
    </div>
    <!--end <div class="row">-->
<?php include(S_ROOT . '/includefiles/bottom.php'); ?>
</body>
</html>